//
//  GetCashVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/29.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "GetCashVC.h"
#import "EditAlipayVC.h"
#import "NSString+Expand.h"
#import "NSUDManager.h"
#import "HelpViewController.h"
#import "CYPasswordView.h"

@interface GetCashVC ()
@property (weak, nonatomic) IBOutlet UILabel *alipayLabel;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;
@property (weak, nonatomic) IBOutlet UILabel *totalPriceLabel;
@property (nonatomic, strong) CYPasswordView *passwordView;
@end

@implementation GetCashVC
- (void)dealloc
{
    [CYNotificationCenter removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    ic_problem
    self.view.backgroundColor = UIColorFromRGB(0XF2F5FA);
    self.navigationItem.title = @"提现";
    [self rightButton];
    self.priceTextField.text = [NSString stringWithFormat:@"%@",@"0"];
    self.totalPriceLabel.text = [NSString stringWithFormat:@"当前可提现金额 %@元",self.totalPrice];
    /** 注册取消按钮点击的通知 */
    [CYNotificationCenter addObserver:self selector:@selector(cancel) name:CYPasswordViewCancleButtonClickNotification object:nil];
    [CYNotificationCenter addObserver:self selector:@selector(forgetPWD) name:CYPasswordViewForgetPWDButtonClickNotification object:nil];
    
}
-(void)cancel{
    
}
-(void)forgetPWD{
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self walletMoney];
}
-(void)walletMoney{
    UserModel *user = [NSUDManager currentUser];
    [YJMSendRequest postDataWithParameters:@{@"userId":user.userId} url:@"user/getUserAlipayAccount" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            if (result[@"data"][@"alipayAccount"] && ![result[@"data"][@"alipayAccount"] isKindOfClass:[NSNull class]]) {
                
                self.alipayLabel.text = [NSString stringWithFormat:@"%@",result[@"data"][@"alipayAccount"]];
            }else{
                self.alipayLabel.text = @"";
            }
            
        }else{
            self.alipayLabel.text = @"";
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    
}
-(void)rightButton{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 100, 44);
    [backBtn setImage:IMAGE(@"ic_problem") forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(getCash) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}
-(void)getCash{
    HelpViewController *vc = [[HelpViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)settingAlipayClick:(id)sender {
    EditAlipayVC *VC = [[EditAlipayVC alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}
- (IBAction)allCashClick:(id)sender {
    self.priceTextField.text = self.totalPrice;
}
- (IBAction)finishClick:(id)sender {
    if (!self.alipayLabel.text || self.alipayLabel.text.length == 0) {
        [CommonUtils showToast:@"请选择提现账号"];
        return;
    }

    if (!self.priceTextField.text || self.priceTextField.text.length == 0) {
        [CommonUtils showToast:@"请输入提现金额"];
        return;
    }
    if (![NSString CheckNumberValueInput:self.priceTextField.text]) {
        [CommonUtils showToast:@"请输入数字"];
        return;
    }
    if ([self.priceTextField.text floatValue] < 50) {
        [CommonUtils showToast:@"最低提现50元"];
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    self.passwordView = [[CYPasswordView alloc] init];
    self.passwordView.title = @"输入提现密码";
    self.passwordView.loadingText = @"提交中...";
    [self.passwordView showInView:self.view.window];
    
    self.passwordView.finish = ^(NSString *password) {
        [weakSelf.passwordView hideKeyboard];
        [weakSelf.passwordView startLoading];
        [weakSelf showProgressWithView:weakSelf.view animated:YES];
        [YJMSendRequest postDataWithParameters:@{@"payPwd":password} url:@"user/checkPayPwd" result:^(NSDictionary *result, NSError *error) {
            
            result = [CommonUtils decryptDicWithResult:result];
            if([result[@"result"] integerValue] == 1)
            {
                [YJMSendRequest withdrawDepositWithAmount:weakSelf.priceTextField.text alipayAccount:weakSelf.alipayLabel.text result:^(NSDictionary *result, NSError *error) {
                    [weakSelf hideProgress:weakSelf.view animated:YES];
                    result = [CommonUtils decryptDicWithResult:result];
                    if([result[@"result"] integerValue] == 1)
                    {
                        [weakSelf.passwordView requestComplete:YES message:@"提现申请成功"];
                        [weakSelf.passwordView stopLoading];
                        [weakSelf.passwordView hide];
                        ALERT_VIEW(@"提现申请成功");
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshWallet" object:nil];
                    }else{
                        [CommonUtils showToast:[result objectForKey:@"msg"]];
                    }
                }];
                
            }else{
                [weakSelf hideProgress:weakSelf.view animated:YES];
                [CommonUtils showToast:[result objectForKey:@"msg"]];
                [weakSelf.passwordView stopLoading];
                [weakSelf.passwordView hide];
            }
            
        }];
    };
    
    
//    __weak typeof(self) weakSelf = self;
//    [self showProgressWithView:self.view animated:YES];
//    [YJMSendRequest withdrawDepositWithAmount:self.priceTextField.text alipayAccount:self.alipayLabel.text result:^(NSDictionary *result, NSError *error) {
//        [weakSelf hideProgress:self.view animated:YES];
//        result = [CommonUtils decryptDicWithResult:result];
//        if([result[@"result"] integerValue] == 1)
//        {
//            ALERT_VIEW(@"提现申请成功");
//            [self.navigationController popViewControllerAnimated:YES];
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshWallet" object:nil];
//        }else{
//            [CommonUtils showToast:[result objectForKey:@"msg"]];
//        }
//    }];
//    /** 提现*/
//    + (NSURLSessionDataTask *)withdrawDepositWithAmount:(NSString *)amount alipayAccount:(NSString *)alipayAccount result:(void (^)(NSDictionary *result, NSError *error))block
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
